<?php

class wms_mdl_pickinglist extends dbeav_model
{

 
    public function searchOptions(){
        $parentOptions = parent::searchOptions();
        $childOptions = array(
            'stockapp_bn'=>app::get('base')->_('申请单号'),            
            'delivery_bn'=>app::get('base')->_('发货单号'),
        );
        return $Options = array_merge($parentOptions,$childOptions);
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null)
    {   
        #发货单
        if(isset($filter['stockapp_bn'])){
            $stockapp_bn = $filter['stockapp_bn'];
            $sql = "select pickinglist_id from sdb_wms_pickinglist_line  where  stockapp_bn= '{$stockapp_bn}'";
            
            $_rows = $this->db->select($sql);
            if(!empty($_rows)){
                foreach($_rows as $_orders){
                    $pickinglist_id[] = $_orders['pickinglist_id'];
                }
                $where .= ' AND id IN ('.implode(',', $pickinglist_id).')';
                unset($filter['stockapp_bn']);
            }

        }
        #发货单
        if(isset($filter['delivery_bn'])){
            $delivery_bn = $filter['delivery_bn'];
            $sql = "select pickinglist_id from sdb_wms_pickinglist_line  where  delivery_bn= '{$delivery_bn}'";
            
            $_rows = $this->db->select($sql);
            if(!empty($_rows)){
                foreach($_rows as $_orders){
                    $pickinglist_id[] = $_orders['pickinglist_id'];
                }
                $where .= ' AND id IN ('.implode(',', $pickinglist_id).')';
                unset($filter['delivery_bn']);
            }

        }

        return parent::_filter($filter,$tableAlias,$baseWhere).$where;
    }

    var $has_export_cnf = true;
    public $export_name = '备货审批单';

    function getselectdata($filter, $offset, $limit)
    {
        $list = $this->getList('*', $filter, $offset, $limit);
        $listData=array();
        $i=0;
        foreach ($list as $k => $v) 
        {
            $sql = "select * from (
                        select A.AXCustomer,A.customername,n.material_bn,b.logi_name,ob.`name`,SUM(it.number*M.volume) as tovolume,count(B.delivery_id) qty from sdb_ome_shop A
                        inner join sdb_wms_delivery B on A.shop_id=B.shop_id
                        LEFT JOIN sdb_wms_delivery_items it on B.delivery_id=it.delivery_id
                        LEFT JOIN sdb_material_basic_material N on N.material_bn=it.bn
                        LEFT JOIN sdb_material_basic_material_ext M on M.bm_id=N.bm_id
                        LEFT JOIN sdb_ome_branch ob on ob.branch_id=b.branch_id
                        inner join sdb_wms_pickinglist_line del on B.delivery_id=del.delivery_id
                        where  del.pickinglist_id=".$v['id']."
                        group by A.AXCustomer,A.customername,n.material_bn
                    )T ORDER BY T.qty desc";
            $rows = $this->db->select($sql);
            foreach ($rows as $key => $value) 
            {
                $listData[$i]['pickinglist_bn'] = ($v['pickinglist_bn'] ? $v['pickinglist_bn'] : '-');
                $listData[$i]['status'] = ($v['status'] ? $v['status'] : '-');
                $listData[$i]['operator'] = ($v['operator'] ? $v['operator'] : '-');
                $listData[$i]['create_time'] = ($v['create_time'] ? $v['create_time'] : '-');
                $listData[$i]['remark'] = ($v['remark'] ? $v['remark'] : '-');

                $listData[$i]['AXCustomer'] = ($value['AXCustomer'] ? $value['AXCustomer'] : '-');
                $listData[$i]['customername'] = ($value['customername'] ? $value['customername'] : '-');
                $listData[$i]['material_bn'] = ($value['material_bn'] ? $value['material_bn'] : '-');
                $listData[$i]['logi_name'] = ($value['logi_name'] ? $value['logi_name'] : '-');
                $listData[$i]['name'] = ($value['name'] ? $value['name'] : '-');
                $listData[$i]['tovolume'] = ($value['tovolume'] ? $value['tovolume'] : '-');
                $listData[$i]['qty'] = ($value['qty'] ? $value['qty'] : '-');
                $i=$i+1;
            }
        }
        return $listData;
    }

    function getExportDataByCustom($fields, $filter, $has_detail, $curr_sheet, $start, $end)
    {
        if($curr_sheet == 1){
            $title = array();
            foreach ($this->io_title('apapi') as $k => $v) {
                $title[] = mb_convert_encoding($v, 'GBK', 'UTF-8');
            }
            $data['content']['main'][0] = '"' . implode('","', $title) . '"';
        }
        $list = $this->getselectdata($filter, $start, $end);
        $status_arr= array (
            'unconfirmed' => '新建',
            'confirmed' => '已提交',
            'approved' => '已审核',
          );
        foreach ($list as $key => $value) 
        {
            $list[$key]['create_time'] = $value['create_time']?date('Y-m-d H:i:s', $value['create_time']):'';
            $list[$key]['status'] = $status_arr[$value['status']];
            
        }
        foreach ($list as $k => $aFilter) {
            $exptmp_data = array();
            foreach ($aFilter as $col => $val) {
                $aFilter[$col] = mb_convert_encoding($aFilter[$col], 'GBK', 'UTF-8');
                $exptmp_data[] = $aFilter[$col];
            }
            $data['content']['main'][] = implode(',', $exptmp_data);
        }
        return $data;
    }
    function io_title($filter, $ioType = 'csv')
    {
        switch ($filter) {
            case 'apapi':
                $this->oSchema['csv'][$filter] = array(
                    '*:审批单号' => 'pickinglist_bn',                   
                    '*:状态' => 'status',             
                    '*:操作员' => 'operator',
                    '*:备货日期' => 'create_time',
                    '*:备注' => 'remark',
                    '*:AX客户' => 'AXCustomer',
                    '*:AX客户名称' => 'customername',
                    '*:物料' => 'material_bn',
                    '*:快递' => 'logi_name',
                    '*:店铺' => 'name',
                    '*:体积' => 'tovolume',
                    '*:件数' => 'qty',
                );
                break;
        }
        $this->ioTitle[$ioType][$filter] = array_keys($this->oSchema[$ioType][$filter]);
        return $this->ioTitle[$ioType][$filter];
    }
    public function get_schema_export()
    {
        $schema = array(
            'columns' => array(
                'pickinglist_bn' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '备货单号',
                    'in_list' => true,
                    'default_in_list' => true,
                    'searchtype' => 'has',
                    'filtertype' => 'normal',
                    'filterdefault' => true,
                    'width' => 150,
                    'order' => 20
                ),
                'status' => array (
                    'type' => 'varchar(32)',
                    'default' => 0,
                    'required' => false,
                    'label' => '状态',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 65,
                    'order' => 50
                ),
                'operator' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '操作员',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'create_time' => array(
                    'type' => 'time',
                    'label'=>'备货日期',
                    'in_list' => false,
                    'default_in_list' => false,
                    'order' => 130,
                ),
                'remark' => array (
                    'type' => 'varchar(50)',
                    'required' => false,
                    'editable' => false,
                    'label' => '备注',
                    'in_list' => false,
                    'default_in_list' => false,
                    'width' => 150,
                    'order' => 40
                ),
                'AXCustomer' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => 'AX客户',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'customername' => array (
                    'type' => 'varchar(64)',
                    'required' => false,
                    'editable' => false,
                    'label' => 'AX客户名称',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'material_bn' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '物料',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'logi_name' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '快递',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'name' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '店铺',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'tovolume' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '体积',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
                'qty' => array (
                    'type' => 'varchar(32)',
                    'required' => false,
                    'editable' => false,
                    'label' => '件数',
                    'in_list' => true,
                    'default_in_list' => true,
                    'width' => 150,
                    'order' => 200
                ),
            ),
            'idColumn' => 'name',
            'in_list' => array(
                0 => 'pickinglist_bn',
                1 => 'status',
                2 => 'operator',
                3 => 'create_time',
                4 => 'remark',
                5 => 'AXCustomer',
                6 => 'customername',
                7 => 'material_bn',
                8 => 'logi_name',
                9 => 'name',
                10 => 'tovolume',
                11 => 'qty',
            ),
            'default_in_list' => array(
                0 => 'pickinglist_bn',
                1 => 'status',
                2 => 'operator',
                3 => 'create_time',
                4 => 'remark',
                5 => 'AXCustomer',
                6 => 'customername',
                7 => 'material_bn',
                8 => 'logi_name',
                9 => 'name',
                10 => 'tovolume',
                11 => 'qty',
            ),
        );
        return $schema;
    }



    public function getExportTitle($fields)
    {
        $title = array();
        $main_columns = $this->get_schema_export();
        foreach (explode(',', $fields) as $k => $col) {
            if (isset($main_columns['columns'][$col])) {
                $title[] = "*:" . $main_columns['columns'][$col]['label'];
            }
        }

        return mb_convert_encoding(implode(',', $title), 'GBK', 'UTF-8');
    }
}
